﻿using AspNetCoreMvcPrj.Models;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Suntront.DatabaseAccessor;
using Suntront.DynamicApiController;
using System.Diagnostics;

namespace AspNetCoreMvcPrj.Controllers
{
    [Route("/api/home")]
    public class HomeController : IDynamicApiController
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IRepository<BW_MeterInfo> _meterRepository;
        private readonly IRepository<BW_WorkFlowManage> _flowManage;
        private readonly ISqlRepository _sqlRepository;

        public HomeController(ILogger<HomeController> logger, ISqlRepository sqlRepository, IRepository<BW_MeterInfo> meterRepository, IRepository<BW_WorkFlowManage> flowManage)
        {
            _logger = logger;
            _sqlRepository = sqlRepository;
            _meterRepository = meterRepository;
            _flowManage = flowManage; 
        }

        [Route("Index")]
        public object Index()
        {
            var metercode = "402508050";
            var entity = _meterRepository.Where(x => x.metercode == metercode).AsNoTracking()?.FirstOrDefault();
          //  using(var dbTrans = _sqlRepository.Database.BeginTransaction())
            {
                try
                {
                    if (entity == null)
                    {
                    }
                    else
                    {
                        entity.yccode = metercode + "0011dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd1";
                        _sqlRepository.Context.Update(entity);
                        _sqlRepository.Context.SaveChanges();
                      //  dbTrans.Commit();

                    }
                }catch(Exception ex)
                {
                    //dbTrans.Rollback();
                }
            }
           
            return null ; 
        }

        [Route("flowmanage")]
        public object manage()
        {
            var id = "5";
            var entity = _flowManage.Where(x => x.id == id).AsNoTracking()?.FirstOrDefault(); //.Adapt<BW_WorkFlowManage>();

            if (entity == null)
            {
            }
            else
            {
                entity.name = "dfdfad";
                entity.remark = null;
                entity.workflowcode = "fzapply3453";
                entity.workflowname = "复装申请";               

            }

           using (var dbTrans = _sqlRepository.Database.BeginTransaction())
            {
                try
                {
                    _flowManage.Update(entity);
                   // _sqlRepository.Context.Update(entity);
                    _sqlRepository.Context.SaveChanges();
                    dbTrans.Commit();
                }
                catch (Exception ex)
                {
                    dbTrans.Rollback();
                }
            }

            return null;
        }

    }
}
